/**
 * 病案打印设置 js
 * @author [song cheng lin]
 */
 
 var PrintSetPurposeId=0;  //定义选中图片分类模板id
 var PrintSetPurposeData;  //保存上次点击的打印目的数据
 var PrintSetOrderData;  //保存上次点击的打印类型及顺序
 
(function(){
	var col = [{field:'FORDERBH',align:'center',title:'分类名称',width:150},
	           //{field:'FALLOWTOPRINT',checkbox:true,title:'<input id=\"allowtoprintcheckbox\" type=\"checkbox\">允许打印',width:60,align:'center'}
			   {field:'FALLOWTOPRINT',title:'<input id=\"allowtoprintcheckbox\" type=\"checkbox\">允许打印',width:60,align:'center',
				   formatter: function (value, rec, rowIndex) {
					   return "<input type=\"checkbox\"  name=\"FALLOWTOPRINT\"   value=\"" + rec.FID + "\" >";
				   }
			   }]
	
	//复印打印目的设置
 	$('#MedicalRecordPrintSetPurposeDatagrid').datagrid({ 
 		url : ctx + "/Print/printPurposeSet",
 		toolbar:"#toolbar-MedicalRecordPrintSet",
		fitColumns : true,
		border : false,
		striped : true,
		fit : true,
		singleSelect : true,
		pageSize : 20,
		pageList : [ 20, 30, 50, 100 ],
	    onClickRow : showPrintSetOrder,
		columns : [[{field:'FCODE',align:'center',title:'编码',width:60},
			   		{field:'FNAME',align:'center',title:'目的名称',width:150},
			   		{field:'FZJC',align:'center',title:'助记词',width:60}]]
 	});
 	
 	//打印分类及顺序
 	$('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid({ 
 		toolbar:"#MedicalRecordPrintSetPrintOrder",
		fitColumns : true,
		border : false,
		striped : true,
		fit : true,
		singleSelect : false,
		 checkOnSelect: true, 
		 selectOnCheck: true,
		//onClickRow : loadPersionInfo,
		columns : [col]
 	});
 	
	
})();



//根据复印打印目的编码id加载该借阅类型下的所有打印分类及顺序
function showPrintSetOrder(index,row){
	
	PrintSetPurposeData = row;
	$('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid({     //加载所有的图片分类模板
		url : ctx + "/Print/showPrintSetOrder",
		queryParams : {
			fcode: row.FID
		},
		onLoadSuccess : function(data){
			if(data){
		    	$.each(data.rows,function(index,item){
		    		$('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('checkRow',index);
		    	});
		   	}
			
		},
		onClickRow: function(rowIndex, rowData) { 
			
            //$("#allowtoprintcheckbox").datagrid("unselectAll"); 
            //$("#allowtoprintcheckbox").datagrid("selectRow", rowIndex);  
        },  
        onCheck: function(index, row) {  
            //$("#allowtoprintcheckbox").datagrid("unselectAll");  
            //$("#allowtoprintcheckbox").datagrid("selectRow", index);  
        },  
		onLoadError : errorPromt
	})
}

//打开新增或修改打印目的dialog
function AddMedicalRecordPrintSet(name){
	$('#addMedicalRecordPrintSetPurposeInfo').form('clear'); //清空表单内容
	if (name == "修改") {
		var items = $('#MedicalRecordPrintSetPurposeDatagrid').datagrid('getSelected');
		if (items == null) {
			$.messager.alert('提示','请选择相应的数据进行修改','info');
			return;
		}else{
			PrintSetPurposeData = items;
			PrintSetPurposeId = items.FID;
			$('#addMedicalRecordPrintSetPurposeInfo').form('load',items);  //将数据放置form表单中
		}
	}
	$('#addMedicalRecordPrintSetPurpose-dialog').dialog({
		closed : false,
		modal : true,
		title : name+'模板',
		buttons : [{
			text: name,
			iconCls: '',
			handler: function(){confirmOpration(name);}
		},
		{text: '取消',handler: function() {
			$("#addMedicalRecordPrintSetPurpose-dialog").dialog("close");
		}
		}]
	})
}
function confirmOpration(name){
	
	var FCODE = $('input[name="FCODE"]').val(); //目的编号
	var FNAME = $('#addMedicalRecordPrintSetPurposeName').val(); //目的名称
	var FZJC = $('#addMedicalRecordPrintSetPurposeZjc').val(); //助记词
	if (""==FCODE || FNAME==null) {
		$.messager.alert('提示','模板编码不能为空','info');
		return;
	}
	if (regNumAndLetter(FCODE) == false) {
   	   	$.messager.alert('提示','目的编号必须由数字或字母组成','info');
		return;
    }
	if (""==FNAME || FNAME==null) {
		$.messager.alert('提示','借阅目的名称不能为空','info');
		return;
	}
	if (CheckStr(FNAME) == false) {
   	   	$.messager.alert('提示','打印目的名称不能包含特殊字符','info');
		return;
    }
	
	if (CheckData(FCODE,FNAME,name,PrintSetPurposeData)) {
		$.messager.alert('提示',"存在相同的编码或名称,请修改",'info');
		return;
	}
	var urlStr = "";
	var data = "";
	if (name == "新增") {
		urlStr = "addPrintPurpose";
		data = $('#addMedicalRecordPrintSetPurposeInfo').serialize();
	}else{
		urlStr = "editPrintPurpose";
		data = $('#addMedicalRecordPrintSetPurposeInfo').serialize()+"&FID="+PrintSetPurposeId;
	}
	
    $.ajax({
		url : ctx +"/Print/"+urlStr,
		dataType : 'json',
		type : 'post',
		data : data,
		success : function(data){
			if (data) {
				$.messager.alert('提示',name+'成功','info');
				$('#MedicalRecordPrintSetPurposeDatagrid').datagrid('reload');
				$("#addMedicalRecordPrintSetPurpose-dialog").dialog("close");
			}else{
				$.messager.alert('提示',name+'失败','info');
				$("#addMedicalRecordPrintSetPurpose-dialog").dialog("close");
			}
		},error : errorPromt
	})
}
//删除打印目的类型
function DeleteMedicalRecordPrintSet(){
	var items = $('#MedicalRecordPrintSetPurposeDatagrid').datagrid('getSelected');
	if (items.length == 0) {
		$.messager.alert('提示','请先选择要删除的打印目的','info');
		return;
	}
	var FID = items.FID; //获取打印目的id
	$.messager.confirm('确认对话框', '确定要删除该条记录吗？', function(r){
		if (r){
			$.ajax({
	 			url : ctx +"/Print/deletePrintPurpose",
	 			dataType : 'json',
	 			type : 'post',
	 			data : {FID : FID},
	 			success : function(data){
	 				if (data) {
	 					$.messager.alert('提示','删除成功','info');
	 					$('#MedicalRecordPrintSetPurposeDatagrid').datagrid('reload');
	 				}else{
	 					$.messager.alert('提示','删除失败','info');
	 				}
	 			},error : errorPromt
 		    })
		}
	});
}




//检测打印目的类型下是否存在相同的编码、名称
function CheckData(FCODE,FNAME,name,items){
    var bhCount=0;
    var nameCount=0;
    var b=true;
    $.ajax({
    	url : ctx +"/Print/checkAllPrintPurpose",
    	async : false,
		dataType : 'json',
		type : 'post',
		success : function(data){
			for (var i = 0; i < data.length; i++) {
				if (name=="新增") {  //新增图片分类模板检测编码、名称和排序号
					if (FCODE == data[i].FCODE) {
						bhCount ++;
						break;
					}
					if (FNAME == data[i].FNAME) {
						nameCount ++;
						break;
					}
				}else{				//修改图片分类模板检测编码、名称和排序号
					if (FCODE == data[i].FCODE && FCODE != items.FCODE) { //排除修改前的编码
						bhCount ++;
						break;
					}
					if (FNAME == data[i].FNAME && FNAME != items.FNAME) { //排除修改前的名称
						nameCount ++;
						break;
					}
				}
			}
			
		    //判断编码，名称和排序是否唯一
		    if (bhCount !=0 || nameCount != 0) {
				b = true;
		    }else{
		    	b = false;
		    }
		},error : errorPromt
    })
	return b;
}



//打印分类及顺序  0上移  1下移
function MedicalRecordPrintSetPrintOrder_MoveLevel(type){
	var items = $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getSelected');
	if (items == null) {
		return;
	}
	//获取选择行的元素下标
	var index = $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getRowIndex',items);
	
	//获取所有行的长度
	var rows = $("#MedicalRecordPrintSetPrintOrderDatagrid").datagrid('getRows').length;
	//执行上移方法
	if (type == 0) {
		if (index != 0) {
			var toup = $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index];
            var todown = $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index - 1];
            
            //执行交换两者的fpx方法
            ExchangeFpx(toup.FID, toup.FPX, todown.FID, todown.FPX);
            
            var toupName = toup.FNAME;
            var toupFpx = toup.FPX;
            //交换两者的名称
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index].FNAME = todown.FNAME;
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index - 1].FNAME = toupName;
            
            //交换两者的排序号
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index].FPX = todown.FPX;
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index - 1].FPX = toupFpx;
            
            //刷新行，并且选中刚操作的行
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('refreshRow', index);
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('refreshRow', index - 1);
            
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('selectRow', index - 1);
            //自动执行点击方法
            loadExaminePersion(index-1, $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index - 1]);
		}
	}else{
		if (index != rows - 1) {
			var todown = $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index];
            var toup = $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index + 1];
            
            //执行交换两者的fpx方法
            ExchangeFpx(todown.FID, todown.FPX, toup.FID, toup.FPX);
            
            var toupName = toup.FNAME;
            var toupFpx = toup.FPX;
            //交换两者的名称
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index + 1].FNAME = todown.FNAME;
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index].FNAME = toupName;
            
            //交换两者排序号
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index + 1].FPX = todown.FPX;
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index].FPX = toupFpx;
            
            //刷新行，并且选中刚操作的行
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('refreshRow', index);
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('refreshRow', index + 1);
            $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('selectRow', index + 1);
            
            //自动执行点击方法
			loadExaminePersion(index+1, $('#MedicalRecordPrintSetPrintOrderDatagrid').datagrid('getData').rows[index + 1]);
		}
	}
}

//上移和下移时交换两者的fpx号 (当前行id，当前行fpx号，交换行id，交换行fpx号)
function ExchangeFpx(currentId, currentFpx, nextId, nextFpx){
	$.ajax({
		url : ctx +"/systemMaintenance/ExchangeFpx",
    	async : false,
		dataType : 'json',
		type : 'post',
		data : {currentId: currentId, currentFpx: currentFpx, nextId: nextId, nextFpx: nextFpx},
		error : errorPromt
	})
}




function loadPersionInfo(index, row){
	

	//var khArray = row.fspks.replace(new RegExp(/(')/g),'').split(',');  //分割科号
	
}